package vista.paneles;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import java.awt.event.MouseListener;

import javax.swing.tree.DefaultMutableTreeNode;

import java.util.List;

import logica.Atributo;


@SuppressWarnings("serial")
public class PanelAtributo extends JPanel {
	
	private TreeDeAtributos listAtributos;
	
	private JButton buttonNuevo;
	private JButton buttonEditar;
	private JButton buttonEliminar;
	
	public PanelAtributo(String titulo) 
	{
		super();
		
		this.setBorder(BorderFactory.createTitledBorder(titulo));
		
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{160, 110};
		gridBagLayout.rowHeights = new int[]{0, 0, 0};
		gridBagLayout.columnWeights = new double[]{1.0, 1.0};
		gridBagLayout.rowWeights = new double[]{1.0, 1.0, 1.0};
		this.setLayout(gridBagLayout);
		
		agregarLista();
		agregarBotonNuevo();
		agregarBotonEditar();
		agregarBotonEliminar();
		
		setVisibilidad();
	}
	
	/*para datos*/
	
	public List<Atributo> getAtributos()
	{
		return this.listAtributos.getAtributos();
	}
	
	public Atributo getSeleccion()
	{ 
		return this.listAtributos.getAtributoSeleccionado();
	}
	
	/*para eventos*/
	
	public void escucharEventosBotonNuevo(MouseListener controlador)
	{
		this.buttonNuevo.addMouseListener(controlador);
	}
	
	public void escucharEventosBotonEditar(MouseListener controlador)
	{
		this.buttonEditar.addMouseListener(controlador);
	}
	
	public void escucharEventosBotonEliminar(MouseListener controlador)
	{
		this.buttonEliminar.addMouseListener(controlador);
	}
	
	public void agregarAtributo(Atributo atributo)
	{
		this.listAtributos.agregarAtributo(atributo);
	}
	
	public void agregarAtributos(List<Atributo> atributos)
	{
		this.listAtributos.agregarAtributosAlArbol(atributos);
	}

	public void quitarAtributo() throws NoEsNodoPadre
	{
		this.listAtributos.quitarAtributo();
	}
	
	public void editarAtributo(Atributo atributoEditado)
	{
		this.listAtributos.editarAtributo(atributoEditado);
	}
	
	public void setEnabled(boolean enabled)
	{
		super.setEnabled(enabled);
		listAtributos.setEnabled(enabled);
		buttonNuevo.setEnabled(enabled);
		buttonEditar.setEnabled(enabled);
		buttonEliminar.setEnabled(enabled);
		
		this.setVisibilidad();
	}
	
	private void setVisibilidad()
	{
		DefaultMutableTreeNode node = (DefaultMutableTreeNode) listAtributos.getLastSelectedPathComponent();

		if (node == null) return;
		{
			this.buttonEditar.setEnabled(false);
			this.buttonEliminar.setEnabled(false);	
		}
	}
	
	/*para dibujar*/
	
	private void agregarBotonEliminar() 
	{
		buttonEliminar = new JButton("Eliminar");
		
		GridBagConstraints gbc_btnEliminar = new GridBagConstraints();
		gbc_btnEliminar.anchor = GridBagConstraints.BELOW_BASELINE;
		gbc_btnEliminar.insets = new Insets(1, 2, 0, 2);
		gbc_btnEliminar.fill = GridBagConstraints.HORIZONTAL;
		gbc_btnEliminar.gridx = 1;
		gbc_btnEliminar.gridy = 2;
		
		add(buttonEliminar, gbc_btnEliminar);
	}

	private void agregarBotonEditar()
	{
		buttonEditar = new JButton("Editar");
		
		GridBagConstraints gbc_btnEditar = new GridBagConstraints();
		gbc_btnEditar.anchor = GridBagConstraints.BELOW_BASELINE;
		gbc_btnEditar.fill = GridBagConstraints.HORIZONTAL;
		gbc_btnEditar.insets = new Insets(1, 2, 5, 2);
		gbc_btnEditar.gridx = 1;
		gbc_btnEditar.gridy = 1;
		
		add(buttonEditar, gbc_btnEditar);
	}

	private void agregarBotonNuevo() 
	{
		buttonNuevo = new JButton("Nuevo");
		
		GridBagConstraints gbc_btnNuevo = new GridBagConstraints();
		gbc_btnNuevo.anchor = GridBagConstraints.BELOW_BASELINE;
		gbc_btnNuevo.fill = GridBagConstraints.HORIZONTAL;
		gbc_btnNuevo.insets = new Insets(1, 2, 5, 2);
		gbc_btnNuevo.gridx = 1;
		gbc_btnNuevo.gridy = 0;
		
		add(buttonNuevo, gbc_btnNuevo);
	}

	private void agregarLista()
	{
		listAtributos = new TreeDeAtributos(new DefaultMutableTreeNode("atributos"));
		
		GridBagConstraints gbc_list = new GridBagConstraints();
		gbc_list.gridheight = 3;
		gbc_list.insets = new Insets(5, 10, 5, 10);
		gbc_list.fill = GridBagConstraints.BOTH;
		gbc_list.gridx = 0;
		gbc_list.gridy = 0;
		
		this.add(new JScrollPane(listAtributos), gbc_list);
	}
}
